{extend name="public:base"}
{block name='css'}
{/block}

{block name='body'}
<div class="view view-main white-navbar white-toolbar">
    <div data-name="home" class="page">
        <div class="navbar">
            <div class="navbar-inner">
                <div class="left"><a href="{$back}" class="external"><i class="iconfont icon-fanhui"></i></a></div>
                <div class="center"><span>添加收货地址</span></div>
                <div class="right"></div>
            </div>
        </div>

        <div class="page-content padding-bottom50 ">

            <form name="form" action="" id="form" method="post">
                <input type="hidden" name="id" value="{$info.id|default=0}"/>
                <div class="list inline-labels no-hairlines-md addlis" style="margin-top:10px;">
                    <ul>
                        <li class="item-content item-input">
                            <div class="item-inner">
                                <div class="item-title item-label">收货人</div>
                                <div class="item-input-wrap">
                                    <input type="text" name="name" value="{$info.name|default=''}" placeholder="请输入收货人姓名">
                                    <span class="input-clear-button"></span>
                                </div>
                            </div>
                        </li>
                        <li class="item-content item-input">
                            <div class="item-inner">
                                <div class="item-title item-label">联系电话</div>
                                <div class="item-input-wrap">
                                    <input type="text" name="tel" value="{$info.tel|default=''}" placeholder="请输入联系电话">
                                    <span class="input-clear-button"></span>
                                </div>
                            </div>
                        </li>
                        <li class="item-content item-input">
                            <div class="item-inner">
                                <div class="item-title item-label">所在地区</div>
                                <div class="item-input-wrap js-region" style="display:flex;">
                                    <select name="sheng_id" onchange="getRegion(this, 1)">
                                        <option value="1">请选择省</option>
                                    </select>
                                    <select name="city_id" onchange="getRegion(this, 2)">
                                        <option value="1">请选择市</option>
                                    </select>
                                    <select name="area_id" onchange="getRegion(this, 3)">
                                        <option value="1">请选择区</option>
                                    </select>
                                </div>
                            </div>
                        </li>
                        <input type="hidden" name="sca_name" value="{$info.sca_name|default=''}"/>
                        <li class="item-content item-input">
                            <div class="item-inner">
                                <div class="item-title item-label">详细地址</div>
                                <div class="item-input-wrap">
                                    <input type="text" name="address" value="{$info.address|default=''}" placeholder="请输入详细地址">
                                    <span class="input-clear-button"></span>
                                </div>
                            </div>
                        </li>
                        <li class="item-content item-input">
                            <div class="item-inner">
                                <div class="item-title item-label">默认地址</div>
                                <div class="item-input-wrap">
                                    <label class="toggle toggle-init color-green">
                                        <input type="checkbox" name="is_default" value="1" {if condition="!isset($info.is_default) || $info.is_default eq 1"}checked="checked"{/if} >
                                        <span class="toggle-icon"></span>
                                    </label>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
                <div class="block">
                    <p class="row segmented-round">
                        <button onclick="" class="col button button-big button-fill " style="background:#66ba61;">保存</button>
                    </p>
                </div>
            </form>

        </div>



    </div>
</div>
{/block}

{block name="js"}
<script>
    $(function(){
        /*{notempty name='info'}*/
        /*{gt name='info.sheng_id' value='0'}*/
        getRegion(0, 0, {$info.sheng_id|default=0});
        /*{gt name='info.city_id' value='0'}*/
        getRegion({$info.sheng_id|default=0}, 1, {$info.city_id|default=0});
        /*{gt name='info.area_id' value='0'}*/
        getRegion({$info.city_id|default=0}, 2, {$info.area_id|default=0});
        /*{/gt}*/
        /*{/gt}*/
        /*{/gt}*/
        /*{else/}*/
        getRegion(0,0);
        /*{/notempty}*/

        var ajaxOn = false;
        $("#form").validate({
            rules: {
                name: "required" ,
                tel: "required" ,
                sheng_id: "required" ,
                city_id: "required" ,
                area_id: "required" ,
                address: "required"
            },
            messages: {
                name: '请输入收货人姓名',
                tel: '请输入联系电话',
                sheng_id: '请选择省',
                city_id: '请选择市',
                area_id: '请选择区',
                address: '请输入详细地址'
            },
            showErrors: function (errorMap, errorList) {
                var msg = "";
                $.each(errorList, function (i, v) {
                    var toastCenter = app.toast.create({
                        text: v.message,
                        position: 'center',
                        closeTimeout: 2000
                    });
                    toastCenter.open();
                    return false;
                });
            },
            onfocusout: false,
            onsubmit : true ,
            onkeyup : false ,
            onclick : false ,
            focusInvalid:false,
            submitHandler:function(form){
                if (ajaxOn) {
                    return false;
                }
                ajaxOn = true;
                $.post("{:url('user/save_address')}", $('#form').serialize(), function(data){
                    ajaxOn = false;
                    messageToast(data.info);
                    if (data.status == 'y') {
                        setTimeout(function () {
                            self.location = "/address";
                        }, 1000);
                    }
                }, 'json');
                return false ;
            }
        });
    });
</script>
{/block}